package com.hyts.threads.core;

import com.hyts.threads.factory.ScheduledThreadPoolFactory;
import com.hyts.threads.service.ThreadPoolService;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author libo
 * @date 2017-07-10
 * @description 任务调度线程管理器
 */

public class ScheduleThreadManager {
		
		
		private static final long DEFAULT_SCHEDULE_DELAY_TIME = 1000L;
		
		private static final TimeUnit DEFAULT_SCHEDULE_TIME_UNIT = TimeUnit.MILLISECONDS;
	
		private static final long DEFAULT_SCHEDULE_REPEATE_TIME = 2000L;
		
		private static ScheduleThreadManager manager;

		/**
		 * 获取实例对象
		 * @param singleton 是否单例
		 * @return
		 */
		public static ScheduleThreadManager getInstance(boolean singleton){
			return singleton ? (manager == null?(manager = new ScheduleThreadManager()): manager):new ScheduleThreadManager();
		}
		
		/**
		 * 执行系统创建任务调度线程
		 * @param corePoolSize 核心线程池
		 * @param delay 延迟时间 执行
		 * @param unit 时间单位
		 * @return
		 */
		public <T>ScheduledExecutorService createScheduleThread(int corePoolSize,long delay,TimeUnit unit,ThreadPoolService<T> threadPoolService){
			delay = delay == 0 ? DEFAULT_SCHEDULE_DELAY_TIME:delay;
			unit = unit == null ? DEFAULT_SCHEDULE_TIME_UNIT : unit;
			ScheduledExecutorService serviceResult = ScheduledThreadPoolFactory.createScheduleTask(corePoolSize);
			serviceResult.schedule(()->{
				threadPoolService.execute();
			}, delay,unit);
			return serviceResult;
		}
		
		/**
		 * 执行系统创建任务调度线程
		 * @param delay
		 * @param unit
		 * @param threadPoolService
		 */
		public <T>ScheduledExecutorService createScheduleThread(long delay,TimeUnit unit,ThreadPoolService<T> threadPoolService){
			delay = delay == 0 ? DEFAULT_SCHEDULE_DELAY_TIME:delay;
			unit = unit == null ? DEFAULT_SCHEDULE_TIME_UNIT : unit;
			ScheduledExecutorService serviceResult = ScheduledThreadPoolFactory.createScheduleTask(0);
			 serviceResult.schedule(()->{
					threadPoolService.execute();
			}, delay, unit);
			 return serviceResult;
		}
		
		/**
		 * 执行系统创建任务调度线程
		 * @param threadPoolService
		 */
		public <T>ScheduledExecutorService  createScheduleThread(ThreadPoolService<T> threadPoolService){
			ScheduledExecutorService serviceResult = ScheduledThreadPoolFactory.createScheduleTask(1);
			 serviceResult.schedule(()->{
					threadPoolService.execute();
			}, DEFAULT_SCHEDULE_DELAY_TIME, DEFAULT_SCHEDULE_TIME_UNIT);
			 return serviceResult;
		}
		
		/**
		 * 执行系统创建任务调度线程
		 * @param delay 延迟执行操作
		 * @param repeat 循环执行时间间隔
		 * @param unit 时间单位 总体
		 * @param threadPoolService 业务逻辑实现接口
		 * @return
		 */
		public <T>ScheduledExecutorService createScheduleThread(long delay,long repeat,TimeUnit unit,ThreadPoolService<T> threadPoolService){
			delay = delay == 0 ? DEFAULT_SCHEDULE_DELAY_TIME:delay;
			repeat = repeat == 0 ? DEFAULT_SCHEDULE_REPEATE_TIME:repeat;
			unit = unit == null ? DEFAULT_SCHEDULE_TIME_UNIT : unit;
			ScheduledExecutorService serviceResult = ScheduledThreadPoolFactory.createScheduleTask(0);
			serviceResult.scheduleWithFixedDelay(new TimerTask(){
				@Override
				public void run() {
					threadPoolService.execute();
				}
			},delay,repeat,unit);
			return serviceResult;
		}
		
		
		public boolean kill(ScheduledExecutorService service){
				if(service == null)
					return false;
				else if(!service.isShutdown()){
					service.shutdown();
					return true;
				}
				else{
					return true;
				}
		}

}
